// exchange.js

const util = require('../../utils/util.js') 
var pageTotal =0;

Page({
  data:{
    userIn:true,
    pageNumber: 1,
    option:"",
    goodlists:[
  // { numbers: "15237665606", names: "迷你小红包", messages: "迷你小红包", values: "50", money: '0', limit: 0, remain: 1, proid: '11'}
    ]
  },
  onLoad:function(option){
    console.log(option)
    this.setData({
      option:option
    })
    this.getAjax()
  },
  onShow:function(){
  },
  listtap:function(e){
    console.log('跳转详情页面');
    // console.log(e.currentTarget.dataset.proid);
    const proid = e.currentTarget.dataset.proid;
    wx.navigateTo({
      url: '../integral_detail/integral_detail?proid=' + proid
    })
  },
  checkimg:function(p){
    console.log(p.length)
      if(p.length>0){
        return p[0].image_url;
      }else{
        return '';
      }
  },
  getAjax:function(){
    var that = this;
    if(this.data.option.type ==1){
     var sendData = {
        "pages_size": "20",
        "pages_number": that.data.pageNumber,
        "exchange_type_id":1
      }
    }else{
      var sendData = {
        "pages_size": "20",
        "pages_number": that.data.pageNumber,
        "area_id": this.data.option.area_id,
        "exchange_type_id": this.data.option.fid
      }
    }
    
    util.getData('products/findProductsList',sendData,'123').then(function (res) {
      console.log(res.data)
       pageTotal = Math.ceil(res.data.total/20)
       const _data = res.data.data.productsList;
       var arrlists = that.data.goodlists;
       for (var i = 0; i < _data.length;i++){
         var index =i
         var str = {};
         str.img =that.checkimg(_data[index].picList);  //图片
         str.numbers = _data[index].pro_num;  //编号
         str.names = _data[index].title;  //名称
         str.messages = _data[index].desc; //简介
         str.values = _data[index].score;  //积分
         str.money = _data[index].money; // 是否加钱,加钱加多少
         str.limit = _data[index].is_once;  //是否限购一次
         str.remain = _data[index].is_sold_out; //是否售罄
         str.proid = _data[index].pro_id; //商品id
         arrlists.push(str)
       }
       that.setData({
         goodlists:arrlists
       })
       // 隐藏加载框  
       wx.hideLoading();  
     })
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },
   onReachBottom: function () { //上滑加载
    var that = this;
    if (pageTotal > that.data.pageNumber ){
      // 显示加载图标  
      wx.showLoading({
        title: '玩命加载中',
      });
      // 页数+1
      that.setData({
        pageNumber: that.data.pageNumber + 1
      })  
      // pageNumber = 
      that.getAjax()
    }else{
      wx.showModal({
        title: "友情提示",
        content: "亲，已经没有更多数据了！",
        showCancel: false,
        confirmText: "好的"
      })
    }   
  }
})